package com.site.dao.impl.master;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.site.dao.master.PropertyDao;
import com.site.dto.master.PropertyBizBean;
import com.site.model.master.PropertyInfo;
import com.site.util.StringUtils;
import com.site.util.base.dao.BaseDaoImpl;
import com.site.util.base.dao.HQLHelper;
import com.site.util.extremetable.QueryPage;

/**
 * property数据访问类
 * 
 * @author 
 */
@Repository
public class PropertyDaoImpl extends BaseDaoImpl implements PropertyDao {

	/* (non-Javadoc)
	 * @see com.site.dao.impl.master.PropertyDao#getAllProperty()
	 */
	@Override
	public List<PropertyInfo> getAllProperty() {
		HQLHelper hqlHelper=new HQLHelper("PropertyInfo","property");	
		hqlHelper.addEqual("markDelete", "N");
		String hql=hqlHelper.getHQL();
		return this.findByHQL(hql,null);
	}
	
	/* (non-Javadoc)
	 * @see com.site.dao.impl.master.PropertyDao#getAllProperty(java.lang.String, java.lang.String, com.site.dto.master.PropertyBizBean, com.site.util.extremetable.QueryPage)
	 */
	@Override
	public List<PropertyInfo> getAllProperty(PropertyBizBean property, QueryPage queryPage) {
		String hql = "";
		HQLHelper hqlHelper=new HQLHelper("PropertyInfo","property");
		hqlHelper.addEqual("markDelete", "N");
		if(null != property){
			if(!StringUtils.isEmpty(property.getPropertyKey()))
				hqlHelper.addLike("propertyKey", property.getPropertyKey());
			if(!StringUtils.isEmpty(property.getRemark())&&StringUtils.isEmpty(property.getRemark1()))
				hqlHelper.addLike("remark", property.getRemark());
			else if(property!=null&&!StringUtils.isEmpty(property.getRemark1())&&StringUtils.isEmpty(property.getRemark()))
				hqlHelper.addLike("remark", property.getRemark1());
			else if(property!=null&&!StringUtils.isEmpty(property.getRemark1())&&!StringUtils.isEmpty(property.getRemark())){
				hql=hqlHelper.getHQL();
				hql += " and (remark like '%"+property.getRemark1()+"%' or remark like '%"+property.getRemark()+"%') ";
			}
		}
		if(StringUtils.isEmpty(hql))
			hql=hqlHelper.getHQL();
		hql=hql + " order by property.createdDate desc";
		if (queryPage != null) {
			return this.findByHQL(queryPage, hql,null);
		} 
		return this.findByHQL(hql, null);
	}
	
	/* (non-Javadoc)
	 * @see com.site.dao.impl.master.PropertyDao#getPropertyKey(java.lang.String)
	 */
	@Override
	public PropertyInfo getPropertyKey(String propertyKey)
	{
		HQLHelper hqlHelper=new HQLHelper("PropertyInfo","property");
		hqlHelper.addEqual("propertyKey", propertyKey);
		hqlHelper.addEqual("markDelete", "N");
		String hql=hqlHelper.getHQL();
		return (PropertyInfo)this.findByHQL(hql,null).get(0);
	}
	
	/* (non-Javadoc)
	 * @see com.site.dao.impl.master.PropertyDao#isExistsProertyKey(java.lang.String, int)
	 */
	@Override
	public boolean isExistsProertyKey(String propertyKey,int propertyId)
	{
		String sql = "";
		if(propertyId==0)
			sql = "select count(PROPERTY_ID) from m_property where MARK_DELETE='N' and PROP_KEY='"+propertyKey+"'";
		else
			sql = "select count(PROPERTY_ID) from m_property where MARK_DELETE='N' and PROP_KEY='"+propertyKey+"' and PROPERTY_ID<>"+propertyId;
		if(this.getCountBySQL(sql)>0)
			return true;
		return false;
	}
	
	/* (non-Javadoc)
	 * @see com.site.dao.impl.master.PropertyDao#getPropertyById(int)
	 */
	@Override
	public PropertyInfo getPropertyById(int propertyId) {
		return (PropertyInfo)this.getEntityById(PropertyInfo.class,propertyId);
	}
	
	/* (non-Javadoc)
	 * @see com.site.dao.impl.master.PropertyDao#tombstoneProperty(int)
	 */
	@Override
	public int tombstoneProperty(int codeId)
	{
		String sql = "update m_property set MARK_DELETE='Y' where PROPERTY_ID="+codeId;
		return this.executeBySQL(sql);
	}
	
	/* (non-Javadoc)
	 * @see com.site.dao.impl.master.PropertyDao#findAllProperty()
	 */
	@Override
	public List<String> findAllProperty() {
		String sql = "select t.remark from m_property t where t.mark_delete='N' group by t.remark";
		List<Object[]> list = this.findBySQL(sql);
		List<String> remarkList = new ArrayList<String>();
		for (Object object : list) {
			if(object != null)
				remarkList.add(object.toString());
		}
		return remarkList;
	}

	@Override
	public void markAsDeleted(Integer obj) {
		super.markAsDeleted(new PropertyInfo(), (Integer) obj);
	}

	@Override
	public void update(PropertyInfo po) {
		super.update(po);
	}

	@Override
	public void save(PropertyInfo po) {
		super.save(po);
	}
	
}
